<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\common\library\wechat\WxJsapi;
/**
 * 默认控制器
 * Class Index
 * @package app\api\controller
 */
class Index extends Controller
{
    public function getWxConfig()
    {
        header('Content-Type: application/json; charset=utf-8');
        $url = $_GET['url'];
        $WxJsapi = new WxJsapi("wx72472525a3170e53","408ae225895cae891ecf56e70937cb3e");
        $res = $WxJsapi->getJsapi($url);
        return $this->renderSuccess($res);
    }

    public function getOpenid(){
        // auth.php
        $appId = 'wx72472525a3170e53';
        $redirectUri = urlencode('https://dcseller.kenleader.com/index.php?s=/api/user/getopenid'); // 回调URL，用户授权后微信会重定向到这个URL
        $state = $_GET['id']; // 用户id
        // 构造OAuth2.0授权链接
        $authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appId}&redirect_uri={$redirectUri}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect";
        // 重定向到OAuth2.0授权链接
        header("Location: {$authUrl}");
        exit;
    }
    public function getcode(){
        // auth.php
        $appId = 'wx72472525a3170e53';
        $redirectUri = urlencode('https://dingcan.kenleader.com/api/user/getopenid'); // 回调URL，用户授权后微信会重定向到这个URL
        $state = $_GET['id']; // 用户id
        // 构造OAuth2.0授权链接
        $authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appId}&redirect_uri={$redirectUri}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect";
        // 重定向到OAuth2.0授权链接
        header("Location: {$authUrl}");
        exit;
    }

    //用于前端获取openid
    public function wechat(){
        // auth.php
        $appId = 'wx72472525a3170e53';
        $state = $_GET['id']; // 用户id
        $redirectUri = urlencode('https://dingcan.kenleader.com/index.php?s=/api/StoreUser/staffauth&id='.$state); // 回调URL，用户授权后微信会重定向到这个URL

        // 构造OAuth2.0授权链接
        $authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appId}&redirect_uri={$redirectUri}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect";
        // 重定向到OAuth2.0授权链接
        header("Location: {$authUrl}");
        exit;
    }
}